package com.devunion.bogdan.view.editors;

import com.devunion.bogdan.view.editors.event.SelectionListener;

import javax.swing.event.EventListenerList;
import java.util.*;
import java.util.List;
import java.awt.*;

/**
 * User: user
 * Date: 06.02.2003
 * Time: 21:26:13
 */
/**
 * Class wor working with component's selection. For ex. if you want to remove all
 * selected components, you can obtain all selected components by
 * calling <a href="#getSelectedComponents()"><code>getSelectedComponents()</code></a> method.
 * Now you know, which components must be removed.
 */
public class SelectionManager {
    private Map<Component, ComponentInfo> component2componentInfo;
    protected EventListenerList listenerList = new EventListenerList();

    public SelectionManager(Map<Component, ComponentInfo> components) {
        this.component2componentInfo = components;
    }

    public List<Component> getSelectedComponents() {
        List<Component> result = new ArrayList<Component>();

        for (Component key : component2componentInfo.keySet()) {
            if (component2componentInfo.get(key).isSelected()) {
                result.add(component2componentInfo.get(key).getOwner());
            }
        }

        return result;
    }

    public void setSelectionForAll(boolean selection) {
        for (Component component : component2componentInfo.keySet()) {
            component2componentInfo.get(component).setSelected(selection);
        }

        fireOnSelectionAllChangeEvent(selection);
    }

    public void setSelection(Component c, boolean selection) {
        component2componentInfo.get(c).setSelected(selection);
        fireOnSelectionChangeEvent(c, selection);
    }

    public boolean getSelection(Component c) {
        return component2componentInfo.get(c).isSelected();
    }

    public void addSelectionListener(SelectionListener listener) {
        listenerList.add(SelectionListener.class, listener);
    }

    public void removeSelectionListener(SelectionListener listener) {
        listenerList.remove(SelectionListener.class, listener);
    }

    public SelectionListener[] getSelectionListeners() {
        return listenerList.getListeners(SelectionListener.class);
    }

    void fireOnSelectionChangeEvent(Component c, boolean selection) {
        // Guaranteed to return a non-null array
        Object[] listeners = listenerList.getListenerList();

        // Process the listeners last to first, notifying
        // those that are interested in this event
        for (int i = listeners.length - 1; i >= 0; i--) {
            if (listeners[i] == SelectionListener.class) {
                ((SelectionListener) listeners[i + 1]).onSelectionChange(c, selection);
            }
        }
    }

    void fireOnSelectionAllChangeEvent(boolean selection) {
        // Guaranteed to return a non-null array
        Object[] listeners = listenerList.getListenerList();

        // Process the listeners last to first, notifying
        // those that are interested in this event
        for (int i = listeners.length - 1; i >= 0; i--) {
            if (listeners[i] == SelectionListener.class) {
                ((SelectionListener) listeners[i + 1]).onSelectionAllChange(selection);
            }
        }
    }
}
